/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ec.socialfun.servicios;

import com.ec.socialfun.entidades.FormasDePago;
import com.ec.socialfun.entidades.Local;
import com.ec.socialfun.entidades.LocalesFormasDePago;
import com.ec.socialfun.enumeradores.EstadoEnum;
import com.ec.socialfun.excepciones.FormaDePagoException;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.jboss.logging.Logger;

/**
 *
 * @author xavier
 */
@Stateless
public class FormasDePagoFacade extends AbstractFacade<FormasDePago> {

    private static final Logger LOG = Logger.getLogger(FormasDePagoFacade.class.getName());
    @PersistenceContext(unitName = "SocialFunPU")
    private EntityManager em;
    @EJB
    private LocalesFormasDePagoFacade localesFormasDePagoFacades;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FormasDePagoFacade() {
        super(FormasDePago.class);
    }

    public List<FormasDePago> recuperaTodasTarjetas() {
        Query q = em.createNamedQuery("FormasDePago.findAll");
        return q.getResultList();
    }

    public boolean verificarExisteEnLocal(Local local, FormasDePago formasDePago) throws FormaDePagoException {

        try {
            LocalesFormasDePago localesFormaPago = localesFormasDePagoFacades.recuperaPorLocalYFormaPago(local, formasDePago);
            return localesFormaPago.getLoFpEstado().equals(EstadoEnum.ACTIVO.getCodigo()) ? true : false;
        } catch (FormaDePagoException e) {
            throw e;
        }
    }

    public List<FormasDePago> recuperaTarjetasAnadidas(Local local) {

        Query q = em.createNamedQuery("FormasDePago.findAll");
        List<FormasDePago> formasDePago = q.getResultList();
        for (FormasDePago obj : formasDePago) {
            try {
                boolean existe = verificarExisteEnLocal(local, obj);
                obj.setExiste(existe);
            } catch (FormaDePagoException ex) {
                obj.setExiste(false);
            }
        }
        return formasDePago;
    }
}
